#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mod_devicetable.h>
#include <linux/gpio/consumer.h>
#include <linux/gpio.h>


/**
 * 目标： 自己编写代码，读取pinctrl-names = "myled1"; ，设置gpio复用
 * 
 * 
 	my_gpio:gpio1_a0 {
	    compatible = "mygpio";
		led1{
			my-gpios = <&gpio1 RK_PA0 GPIO_ACTIVE_HIGH>, <&gpio1 RK_PB1 GPIO_ACTIVE_HIGH>;
			pinctrl-names = "default";
			pinctrl-0 = <&mygpio_ctrl>;		
		};
		led2{
			my-gpios = <&gpio1 RK_PB0 GPIO_ACTIVE_HIGH>;	
		};
	};
    
  问题，上面led1,led2 无法转化为platform_device
  没有用了&dev->dev
  改成下面： 

 * 
 	my_gpio:gpio1_a0 {
    	compatible = "mygpio";
		my-gpios = <&gpio1 RK_PA0 GPIO_ACTIVE_HIGH>;
		pinctrl-names = "myled1";
		pinctrl-0 = <&mygpio_ctrl>;		
	};
 */

struct pinctrl *led_pinctrl; // pinctrl 实例指针
struct pinctrl_state *led_state;// pinctrl 状态指针
int ret;
//平台设备初始化函数
static int my_platform_probe(struct platform_device *dev)
{
    printk("This is mydriver_probe\n");
    
    led_pinctrl = pinctrl_get(&dev->dev);// 获取 pinctrl 实例
    if (IS_ERR(led_pinctrl)) {
        printk("pinctrl_get is error\n");
        return -1;
    }
    
    /**
     * 获取自己定义的状态
     */
    led_state = pinctrl_lookup_state(led_pinctrl, "myled1");// 查找状态
    if (IS_ERR(led_state)) {
        printk("pinctrl_lookup_state is error\n");
        return -2;
    }
    
    /**
     * 把定义的状态设置到硬件
     */
    ret = pinctrl_select_state(led_pinctrl, led_state);// 设置状态到硬件
    if (ret < 0) {
        printk("pinctrl_select_state is error\n");
        return -3;
    }
    
    return 0;
}

// 平台设备的移除函数
static int my_platform_remove(struct platform_device *pdev)
{
    printk(KERN_INFO "my_platform_remove: Removing platform device\n");

    // 清理设备特定的操作
    // ...

    return 0;
}


const struct of_device_id of_match_table_id[]  = {
	{.compatible="mygpio"},
};

// 定义平台驱动结构体
static struct platform_driver my_platform_driver = {
    .probe = my_platform_probe,
    .remove = my_platform_remove,
    .driver = {
        .name = "my_platform_device",
        .owner = THIS_MODULE,
		.of_match_table =  of_match_table_id,
    },
};

// 模块初始化函数
static int __init my_platform_driver_init(void)
{
    int ret;

    // 注册平台驱动
    ret = platform_driver_register(&my_platform_driver);
    if (ret) {
        printk(KERN_ERR "Failed to register platform driver\n");
        return ret;
    }

    printk(KERN_INFO "my_platform_driver: Platform driver initialized\n");

    return 0;
}

// 模块退出函数
static void __exit my_platform_driver_exit(void)
{
    // 注销平台驱动
    platform_driver_unregister(&my_platform_driver);

    printk(KERN_INFO "my_platform_driver: Platform driver exited\n");
}

module_init(my_platform_driver_init);
module_exit(my_platform_driver_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("topeet");
